﻿var CONST = CONST || {};
CONST.params = {};

(function () {
    $.ajax({
        url: '/Model/DisposeType',
        type: 'GET',
        async: false
    }).then(function (data) {
        data.splice(0, 0, { id: null, Name: '' });
        CONST.DisposeTypes = data;
    })
})();

App.KnowledgedetailTranstocreatedetailRoute = Ember.Route.extend({
    setupController: function (controller, model) {
        this.transitionTo('knowledgedetailtype.createdetail', CONST.params.knowledgeDetailType_id)
    }
});

App.KnowledgedetailRoute = Ember.Route.extend({
    model: function (params) {
        CONST.params = params;

        return Ember.$.ajax({
            url: '/Model/KnowledgeDetail?detailType=' + params.knowledgeDetailType_id,
            type: 'GET',
            async: false
        });
    },
    renderTemplate: function (controller, model) {
        controller.set('content', model);
        this.render('knowledgeDetail/list', {
            controller: controller
        });
    },
    serialize: function (model) {
        return {
            knowledgeDetail_id: model.id
        }
    }
});

App.KnowledgedetailtypeCreatedetailRoute = Ember.Route.extend({
    model: function (params, transation) {
        return App.store.createRecord('KnowledgeDetail');
    },
    renderTemplate: function (controller, model) {
        controller.set('content', model);
        //controller.set('disposeTypes', CONST.DisposeTypes);

        if (CONST.params.knowledgeDetailType_id) {
            var model = App.store.getById(App.KnowledgeDetailType, CONST.params.knowledgeDetailType_id);
            controller.set('detailTypes', [model]);
        } else {
            controller.set('detailTypes', []);
        };
        this.render('knowledgeDetail/edit', {
            controller: controller
        });
    },
    actions: {
        save: function (model) {
            var that = this;
            
            var json = model.toJSON();
            json.id = model.id;
            json.Cases = CKEDITOR.instances.iptCases.getData();
            var userData = [];
            $('#d2tcontainer').find('div.pull-left')
                .each(function (i, dom) {
                    var $this = $(this);
                    userData.push($this.data('id'));
                });
            json.userData = $.unique(userData).join(',');

            Ember.$.model.add('KnowledgeDetail', json, function (id) {
                model.set('id', id);
                model.save();

                that.controllerFor('KnowledgeDetail').transitionTo('knowledgedetail',
                    CONST.params.knowledgeDetailType_id || (userData.length > 0 && userData[0]));
            });
        },
        addDetailType: function (model, dt) {
            $.dialog({
                title: '选择知识细类',
                content: $('#selectDetailType').html(),
                lock: true,
                ok: function () {
                    var selected = $('.ui_content').find('select').val();
                    var typeModel = App.store.getById(App.KnowledgeDetailType, selected);

                    //处理UI
                    $('#d2tcontainer').find('a')
                        .before([
                            '<div data-id="' + typeModel.id + '" class="pull-left">',
                                '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>',
                                typeModel.get('Name'),
                            '</div>'
                        ].join(''));
                }
            });
        }
    }
});
App.KnowledgedetailtypeEditdetailRoute = Ember.Route.extend({
    model: function (params) {
        return Ember.$.ajax({
            url: '/Model/KnowledgeDetail/CURD/' + params.knowledgeDetail_id,
            type: 'GET',
            async: false
        });
    },
    renderTemplate: function (controller, model) {
        controller.set('content', model);
        controller.set('disposeTypes', CONST.DisposeTypes);

        Ember.$.ajax({
            url: '/Model/KnowledgeDetail/GetDetial2Type/' + model.id,
            type: 'GET',
            async: false
        }).then(function (data) {
            var arrResult = [];
            $.each(data, function (i, json) {
                var model = App.store.getById(App.KnowledgeDetailType, json.KnowledgeDetailsType_ID);
                arrResult.push(model);
            });
            controller.set('detailTypes', arrResult);
        });

        this.render('knowledgeDetail/edit', {
            controller: controller
        });
    },
	actions: {
	    save: function (model) {
	        var that = this;

	        var json = {};
	        json.id = model.id;
	        json.Title = model.Title;
	        json.Remarks = model.Remarks;
	        json.Cases = CKEDITOR.instances.iptCases.getData();
	        json.DisposeType_ID = model.DisposeType_ID;
	        var userData = [];
	        $('#d2tcontainer').find('div.pull-left')
                .each(function (i, dom) {
                    var $this = $(this);
                    userData.push($this.data('id'));
                });
	        json.userData = $.unique(userData).join(',');

	        Ember.$.model.mod('KnowledgeDetail', json, function (id) {
	            that.controllerFor('KnowledgeDetail').transitionTo('knowledgedetail',
                    CONST.params.knowledgeDetailType_id || (userData.length > 0 && userData[0]));
	        });
	    },
	    addDetailType: function (model, dt) {
	        $.dialog({
	            title: '选择知识细类',
	            content: $('#selectDetailType').html(),
	            lock: true,
	            ok: function () {
	                var selected = $('.ui_content').find('select').val();
	                var typeModel = App.store.getById(App.KnowledgeDetailType, selected);

	                //处理UI
	                $('#d2tcontainer').find('a')
                        .before([
                            '<div data-id="' + typeModel.id + '" class="pull-left">',
                                '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>',
                                typeModel.get('Name'),
                            '</div>'
                        ].join(''));
	            }
	        });
	    }
	}
});
App.KnowledgedetailtypeInfodetailRoute = Ember.Route.extend({
    model: function (params) {
        return Ember.$.ajax({
            url: '/Model/KnowledgeDetail/CURD/' + params.knowledgeDetail_id,
            type: 'GET',
            async: false
        });
    },
    renderTemplate: function (controller, model) {
        controller.set('content', model);
        controller.set('disposeTypes', CONST.DisposeTypes);

        Ember.$.ajax({
            url: '/Model/KnowledgeDetail/GetDetial2Type/' + model.id,
            type: 'GET',
            async: false
        }).then(function (data) {
            var arrResult = [];
            $.each(data, function (i, json) {
                var model = App.store.getById(App.KnowledgeDetailType, json.KnowledgeDetailsType_ID);
                arrResult.push(model);
            });
            controller.set('detailTypes', arrResult);
        });

        var disposeType_ID = model.DisposeType_ID;
        // property being used as a getter
        var json = CONST.DisposeTypes.filter(function (json) {
            return json.id == disposeType_ID;
        })[0];

        controller.set('DisposeType_Name', json ? json.Name : '');

        this.render('knowledgeDetail/detail', {
            controller: controller
        });
    },
    actions: {
        collect: function (model) {
            if (!userJson.id) {
                $.dialog.alert('请先登陆!');
                return;
            }

            var dialog = $.dialog({
                title: '选择要收藏到的分类：',
                content: 'url:/Content/knowledge.assets/html/UserKnowledgeDetailType.Select.html?' + userJson.id,
                width: '300px',
                height: '500px'
            });
            dialog.doOK = function (data) {
                $.ajax({
                    url: '/Model/Details2UserType/',
                    type: 'POST',
                    data: { userID: userJson.id, detailID: model.id, userDetailsType: data.id }
                }).then(function (ar) {
                    if (ar.Success) {
                        dialog.close();
                    }
                });
            };
        }
    }
});